package com.example.vocab.controller;

import com.alibaba.fastjson.JSON;
import com.example.vocab.entity.User;
import com.example.vocab.service.UserService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;

@RestController
public class RegisterController {

    @Resource(name="userServiceImpl")
    UserService userService;

    @RequestMapping("registry")
    public String register(@RequestBody User user)
    {
        HashMap<String, String> resMap = new HashMap<>();
        if(userService.selectUserByUid(user)!=null)
        {
            resMap.put("flag","fail");
            resMap.put("msg","用户已存在！");
        }
        else{
            int i = userService.addUser(user);
            if(i==1){
                resMap.put("flag","success");
                resMap.put("msg","注册成功，请登录！");
            }
            else
            {
                resMap.put("flag","fail");
                resMap.put("msg","error");
            }
        }
        String data= JSON.toJSONString(resMap);
        return data;
    }
}
